package com.yiyuzi.admin.role;

import com.yiyuzi.admin.common.domain.IDQueryDTO;
import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.domain.ResponseDTO;
import com.yiyuzi.admin.common.utils.ConvertUtils;
import com.yiyuzi.admin.role.domain.dto.RoleAddDTO;
import com.yiyuzi.admin.role.domain.dto.RoleDTO;
import com.yiyuzi.admin.role.domain.dto.RoleQueryDTO;
import com.yiyuzi.admin.role.domain.dto.RoleUpdateDTO;
import com.yiyuzi.admin.role.domain.entity.RoleEntity;
import com.yiyuzi.admin.role.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author yiyuzi
 * @date 2021/5/7 11:18
 */

@RestController
@RequestMapping("role")
@Api(tags = "角色")
public class RoleController {
    @Resource
    private RoleService roleService;

    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询")
    public ResponseDTO<RoleDTO> getById(@PathVariable Long id){
        return ResponseDTO.res(ConvertUtils.copy(roleService.getById(id), RoleDTO.class), ResponseCodeEnum.ROLE_NOTFOUND);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public ResponseDTO<String> add(RoleAddDTO roleAddDTO){
        if(roleAddDTO.getRoleName() == null)
            return ResponseDTO.err(ResponseCodeEnum.ROLE_INFO_NULL);
        return ResponseDTO.res(!roleService.save(ConvertUtils.copy(roleAddDTO, RoleEntity.class)));
    }

    @PostMapping("/query/all")
    @ApiOperation(value = "查询全部")
    public ResponseDTO<PageResultDTO<RoleDTO>> getAll(){
        return ResponseDTO.res(roleService.findPage(new RoleQueryDTO()));
    }

    @PostMapping("/query/page")
    @ApiOperation(value = "条件查询")
    public ResponseDTO<PageResultDTO<RoleDTO>> getPage(RoleQueryDTO roleDTO){
        return ResponseDTO.res(roleService.findPage(roleDTO));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新")
    public ResponseDTO<String> updateById(RoleUpdateDTO roleDTO){
        return ResponseDTO.res(roleService.updateById(ConvertUtils.copy(roleDTO, RoleEntity.class)), ResponseCodeEnum.FAIL);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public ResponseDTO<String> deleteById(@PathVariable Long id){
        try {
            return ResponseDTO.res(roleService.deleteById(id));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }

    @PostMapping("/delete/list")
    @ApiOperation(value = "批量删除")
    public ResponseDTO<String> deleteList(IDQueryDTO idQueryDTO){
        try {
            return ResponseDTO.res(roleService.deleteList(idQueryDTO.getIdList()));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出excel")
    public ResponseDTO<String> export() throws IOException {
        return ResponseDTO.res(roleService.export());
    }
}
